package com.akk.business;

import com.akk.module.avs.entity.AvsYbAppoCard;
import com.akk.module.avs.entity.AvsYbCardAccess;
import com.akk.module.avs.entity.AvsYbCardRaider;
import com.akk.module.avs.entity.AvsYbTags;
import com.akk.module.avs.enums.AccessCardEnums;
import com.akk.module.avs.enums.AppoCardStatus;
import com.akk.module.avs.model.response.AvsYbCardMatchRes;
import com.akk.module.avs.service.*;
import com.akk.utils.RestResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;

import java.util.*;
import java.util.stream.Collectors;

@Component
public class AvsYbCardBusiness {

    @Autowired
    private AvsYbCardService avsYbCardService;

    @Autowired
    private AvsYbAppoCardService appoCardService;

    @Autowired
    private AvsYbCardAccessService cardAccessService;

    @Autowired
    private AvsYbTagsService tagsService;

    @Autowired
    private AvsYbCardRaiderService cardRaiderService;

    public RestResponse queryCardTop5(String appId){
        return RestResponse.build(avsYbCardService.queryTop5(appId));
    }

    public RestResponse cardAll(String appId){
        if (StringUtils.isEmpty(appId))
            return RestResponse.build("1","参数缺失",null);
        Map params = new HashMap();
        params.put("appId",appId);
        return RestResponse.build(avsYbCardService.findByParams(params));
    }

    public RestResponse appoCard(AvsYbAppoCard req, BindingResult bindingResult) {
        if (bindingResult.hasErrors())
            return RestResponse.build("1",bindingResult.getFieldError().getDefaultMessage(),"");
        req.setStatus(AppoCardStatus.PENDING.getValue());
        req.setCreateDate(new Date());
        appoCardService.save(req);
        return RestResponse.build("200","success","");
    }

    public RestResponse accessCard(AvsYbCardAccess cardAccess,BindingResult bindingResult){
        if (bindingResult.hasErrors())
            return RestResponse.build("1",bindingResult.getFieldError().getDefaultMessage(),"");
        cardAccess.setStatus(AccessCardEnums.AccessCardStatus.NORMAL.getValue());
        cardAccess.setCreateDate(new Date());
        cardAccessService.save(cardAccess);
        return RestResponse.build("success");
    }

    public RestResponse cardRaider(String appId){
        if (StringUtils.isEmpty(appId))
            return RestResponse.build("1","参数缺失",null);
        Map params = new HashMap();
        params.put("appId",appId);
        List<AvsYbCardRaider> list = cardRaiderService.findByParams(params);
        return RestResponse.build(list);
    }

    public RestResponse match(String appId,List<Long> tagIds){
        Map params = new HashMap();
        params.put("appId",appId);
        params.put("tags",tagIds);
        List<AvsYbCardMatchRes> list = avsYbCardService.seletMatch(params);
        if (list.size() == 0)
            list = avsYbCardService.seletMatchTop5(params);
        if (list.size() == 0)
            return RestResponse.build("1","无数据");
        for (AvsYbCardMatchRes res:list) {
            float m = 100.0f / tagIds.size();
            float match = 0f;
            Map<Long,AvsYbTags> resTags = res.getTags().stream().collect(Collectors.toMap(AvsYbTags::getTagId,(p) -> p));
            for (Long tag:tagIds) {
                if (resTags.containsKey(tag))
                    match += m;
            }
            res.setMatch(match);
        }
//        Collections.sort(list, (o2, o1) -> 0);
        Collections.reverse(list);
        return RestResponse.build(list);
    }
}
